package com.jinghua.treatment.controller;

import com.jinghua.treatment.common.GlobalConstant;
import com.jinghua.treatment.common.controller.BaseController;
import com.jinghua.treatment.common.domain.JSONResult;
import com.jinghua.treatment.entity.Medical;
import com.jinghua.treatment.entity.UsualPrescriptionVO;
import com.jinghua.treatment.entity.vo.PrescriptionVO;
import com.jinghua.treatment.service.PrescriptionService;
import com.jinghua.treatment.wechat.entity.JSONEntity;
import com.sun.xml.internal.rngom.parse.host.Base;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by LV
 */

@Controller
@RequestMapping("/prescription")
public class PrescriptionController extends BaseController{



    @ApiOperation(value ="保存开方信息")
    @RequestMapping(value = "/save")
    @ResponseBody
    public JSONResult savePrescription(PrescriptionVO prescriptionVO) {
        System.out.println(prescriptionVO.getSeeLevel());

        prescriptionService.savePrescription(prescriptionVO);
        return jsonResult(GlobalConstant.SUCCESS_CODE,"ok",null);
    }


    @Autowired
    private PrescriptionService prescriptionService;

    @ApiOperation(value = "查询所有药方数")
    @GetMapping(value = "/countAllPrescriptions")
    @ResponseBody
    public int countAllPrescriptions(HttpServletRequest request) {
        String userId = "123";
        return prescriptionService.countAllPrescriptions(userId);
    }

    @ApiOperation(value = "查询配送中药方数")
    @GetMapping(value = "/countCarryPrescriptions")
    @ResponseBody
    public int countCarryPrescriptions(HttpServletRequest request) {
        String userId = "123";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("userId", userId);
        paramMap.put("state", "3");
        return prescriptionService.countStatePrescriptions(paramMap);
    }


    @ApiOperation(value = "查询所有药方")
    @GetMapping(value = "/selectAllPrescriptions")
    public String selectAllPrescriptions(String state, Model model, HttpServletRequest request) {
        String userId = "123";
      /*  PrescriptionVO param = new PrescriptionVO();
        param.setUserId(userId);
        if (!StringUtils.isEmpty(state)) {
            param.setPrescriptionState(state);
        }
        List<PrescriptionVO> result = prescriptionService.selectAllPrescriptions(param);
        String resultStr = "";
        if (null != result && result.size() > 0) {
            result.forEach(vo -> {
                String stateStr = vo.getPrescriptionState();
                vo.setPrescriptionState(stateStr.equals("1") ? "已支付" : stateStr.equals("2") ? "咨询免费" : stateStr.equals("3") ? "配送中" : "未知");
            });
            model.addAttribute("prescriptionList", result);
            resultStr = "prescription/prescriptionList";
        } else {
            resultStr = "prescription/noPrescriptionList";
        }
        return resultStr;*/
      return "";
    }

    @ApiOperation(value = "查询所有常用方")
    @GetMapping(value = "/selectAllUsualPrescription")
    public String selectAllUsualPrescription(Model model) {
        String userId = "123";
        List<UsualPrescriptionVO> result = prescriptionService.selectAllUsualPrescription(userId);
        if (null != result && result.size() > 0) {
            model.addAttribute("resultList", result);
            return "prescription/usualList";
        } else {
            return "prescription/noPrescriptionList";
        }

    }

  /*  @ApiOperation(value = "添加药方")
    @PostMapping(value = "/addPrescription")
    @ResponseBody
    public String addPrescription(@RequestBody PrescriptionVO param) {
      param.setUserId("123");
      int result = prescriptionService.addPrescription(param);
        if (result > 0) {
            return "{\"success\":1}";
        } else {
            return "{\"false\":1}";
        }
    }*/

    @ApiOperation(value = "添加常用方")
    @PostMapping(value = "/addUsualPrescription")
    @ResponseBody
    public String addUsualPrescription(@RequestBody UsualPrescriptionVO param) {
        param.setUserId("123");
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        param.setPrescriptionTime(myFmt.format(new Date()));
        int result = prescriptionService.addUsualPrescription(param);
        if (result > 0) {
            return "{\"success\":1}";
        } else {
            return "{\"false\":1}";
        }

    }

    @ApiOperation(value = "查询所有中药")
    @PostMapping(value = "/getAllMedicals")
    public String getMedicalByName(String name, Model model) {
        List<Medical> result = null;
        if (StringUtils.isEmpty(name))
            result = prescriptionService.getMedicalByName(null);
        else
            result = prescriptionService.getMedicalByName("%" + name + "%");
        model.addAttribute("resultList", result);
        return "prescription/medicalSearchList";
    }

 /*   @ApiOperation(value = "根据药贴中的药材ID,数量查价格")
    @PostMapping(value = "/getPrescriptionPriceByMedical")
    @ResponseBody
    public Double getPrescriptionPriceByMedical(@RequestBody  List<PrescriptionDetailVO> medicalList) {
        double resultPrice = 0;
        Map param = new HashMap<>();
        param.put("userId","123");
        for (PrescriptionDetailVO medical : medicalList) {
            param.put("medicalId",medical.getMedicalId());
            resultPrice += Double.valueOf(medical.getCount()) * prescriptionService.getPrescriptionPriceByMedical(param);
        }
        return resultPrice;
    }*/
}
